<?php
// app.php
// 2005-2008 Derek "Kazan" Meek
// used by permission

class application
{
	var $sess; // session handler
	var $db;
	
	//--------------- functions ---------------
	function __construct()
	// $site name is used in the session cookie
	{
		global $config;
		$this->sess = new session($config['session_name'], $this);
		
		$db_class = $config['database']['type'] . "_database";
		$this->db = new $db_class(
						$config['database']['host'], 
						$config['database']['user'], 
						$config['database']['pass'], 
						$config['database']['db'], 
						$this);
		if (!$this->db->connect())
		{
			$this->error("Could not connect to database.");
			$this->pg = null;
		}
		else
		{
			$default_page = ifemptyuse($config['defaultpage'], "page");
			if (@$_REQUEST['pgn'])
				$this->pg = new $_REQUEST['pgn']($this);
			else 
				$this->pg = new $default_page($this);
		}
	}

	function run()
	{
		if (!empty($this->pg))
		{
			$ret = $this->pg->pre_process();
			
			if (is_array($ret))
			{
				$status = $ret['status'];
				$msg = $ret['msg'];
			}
			else
			{
				
				$status = $ret;
				$msg = "You are not authorized to view this page.";
			}
			
			if ($status)
			{
				$this->pg->page_do();
			}			
			$this->pg->display(!$status, $msg);
			
		}
	}
	
	function error($msg)
	{
		trigger_error($msg);
	}

}

?>